from rest_framework.views import APIView
from rest_framework import serializers
from orders.models import *
from areass.models import *
from users.models import *

# class AddressSerialzizer(serializers.ModelSerializer):
#     city_id = serializers.StringRelatedField()
#     district_id = serializers.StringRelatedField()
#     province_id = serializers.StringRelatedField()
#
#     class Meta:
#         model = Address
#         fields = '__all__'
#
#
# class Express_OrderGoodsSerialzier(serializers.ModelSerializer):
#     sku_id = serializers.StringRelatedField()
#     class Meta:
#         model = OrderGoods
#         fields = ['sku_id']
#
# class Express_OrderInfoSerializer(serializers.ModelSerializer):
#     address = AddressSerialzizer()
#     class Meta:
#         model = OrderInfo
#         fields = '__all__'
# class CommoditySerialzier(serializers.Serializer):
#     GoodsName = serializers.CharField()


# class UserSerializer(serializers.Serializer):
#     Name = serializers.CharField()
#     Tel = serializers.CharField(required=False)
#     Mobile = serializers.CharField(required=False)
#     ProvinceName = serializers.CharField()
#     CityName = serializers.CharField()
#     ExpAreaName = serializers.CharField()
#     Address = serializers.CharField()
#
#     def validate(self,attrs):
#         if not (attrs.get('Mobile') or attrs.get('Tel')):
#             raise serializers.ValidationError('手机和电话必须要有一个')
#         return attrs
# class CommoditySerializer(serializers.Serializer):
#     GoodsName = serializers.CharField()
#
#     def validate(self,attrs):
#         print(attrs)
#         return attrs
#
#
# class PlaceOrderSerialzier(serializers.Serializer):
#     OrderCode = serializers.CharField()
#     ShipperCode = serializers.CharField()
#     PayType = serializers.CharField()
#     ExpType = serializers.CharField()
#     Sender = UserSerializer()
#     Receiver = UserSerializer()
#     Commodity = CommoditySerializer(many=True)
#     # Commodity = CommoditySerializer()
#     Quantity = serializers.CharField()
#
#     def validate(self, attrs):
#         if attrs.get('shipperCode') == 'ZTO':
#             attrs['CustomerName'] = 'testzto'
#             attrs['CustomerPwd'] = 'testztopwd'
#             return attrs


class UserSerializer(serializers.Serializer):
    Name = serializers.CharField()
    Tel = serializers.CharField(required=False)
    Mobile = serializers.CharField(required=False)
    ProvinceName = serializers.CharField()
    CityName = serializers.CharField()
    ExpAreaName = serializers.CharField()
    Address = serializers.CharField()

    def validate(self, attrs):
        if not (attrs.get('Mobile') or attrs.get('Tel')):
            raise serializers.ValidationError("手机和电话必须有一个！")
        return attrs

class CommoditySerializer(serializers.Serializer):
    GoodsName = serializers.CharField()

class PlaceOrderSerializer(serializers.Serializer):
    OrderCode = serializers.CharField()
    ShipperCode = serializers.CharField()
    PayType = serializers.IntegerField()
    ExpType = serializers.IntegerField()
    Sender = UserSerializer()
    Receiver = UserSerializer()
    Commodity = CommoditySerializer(many=True)
    Quantity = serializers.IntegerField()

    def validate(self, attrs):
        # 如果是中通订单,需要添加客户号
        # 具体哪些物流公司需要客户号请查看《快递鸟电子面单客户号参数对照表.xlsx》
        if attrs.get("ShipperCode") == "ZTO":
            attrs['CustomerName'] = 'testzto'
            attrs['CustomerPwd'] = 'testztopwd'
        return attrs
